import json

import requests


def get_ip():
    kdl_url = "https://dps.kdlapi.com/api/getdps/?secret_id=o6ykpp1tqja9zj9g4947&signature=ne52s9ngdvy61nrghkzf7kytjk7c3qgk&num=1&format=json&sep=1"
    ip = requests.get(kdl_url).json()["data"]["proxy_list"][0]
    proxies = {
        # "http://账户名:密码@IP地址:端口号",
        'http': f'http://d2769292027:8bnrqo2g@{ip}',
        'https': f'http://d2769292027:8bnrqo2g@{ip}',
    }
    return proxies


def get_ip():
    url = "https://find.xiaoxiongip.com/find_ip?key=bb2e7aff713a3e7e&timestamp=1750391358524&signature=500303e951fc71c3bee399dcbb3713ec&count=1&type=json&only=0&province=214&city=215&pw=yes"
    ip = requests.get(url).json()["list"][0]["sever"]
    proxies = {
        # "http://账户名:密码@IP地址:端口号",
        'http': f'http://bb2e7aff713a3e7e:91187777620335@{ip}',
        'https': f'http://bb2e7aff713a3e7e:91187777620335@{ip}',
    }
    return proxies


proxies = get_ip()

headers = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Origin": "https://www.nstl.gov.cn",
    "Referer": "https://www.nstl.gov.cn/resources_search.html?t=DegreePaper",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0",
    "X-Requested-With": "XMLHttpRequest",
    "sec-ch-ua": "\"Microsoft Edge\";v=\"137\", \"Chromium\";v=\"137\", \"Not/A)Brand\";v=\"24\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}
cookies = {
    "NCK": "PYWLWV2LWG5JE5VLH4YRDDWXBMELLJTA",
    "libcode": "CN000000",
    "C": "0",
    "nstl_rsp": "",
    "nstl_token": "564a9e6a-c344-801b-a48b-35d66af97c5d",
    "SESSION": "MTU3MWZjZDQtMGQyZC00MjEwLWI2ZmUtM2FkMDdlYWQ2ODQ0"
}
url = "https://www.nstl.gov.cn/api/service/nstl/web/execute"
params = {
    "target": "nstl4.search4",
    "function": "paper/pc/list/afc",
    "time": "1750298412594",
    "tid": "4839ba50b9e0c4e5e5c0356dba9396a8"
}
data = {
    "query": "{\"c\":10,\"st\":\"0\",\"f\":[\"hasAutnam_s\"],\"p\":\"\",\"q\":[{\"k\":\"uni_s\",\"a\":1,\"o\":\"\",\"f\":1,\"v\":\"山东大学\"}],\"op\":\"AND\",\"s\":[\"nstl\",\"haveAbsAuK:desc\",\"yea:desc\",\"score\"],\"t\":[\"DegreePaper\"],\"fr\":100000}",
    "searchWordId": "f2db4d8323b5479b4a61eddd97fcd635",
    "searchId": "16b0f44af185fc06f917dededec7cca0",
    "facetId": "dbeda112754241c54cc9ef9305259cc0"
}
response = requests.post(url, headers=headers, cookies=cookies, params=params, data=data, proxies=proxies)
counter = 0
print(proxies)
print(response.content.decode())
result = response.json()["facet"]
with open("author.json", "w", encoding="utf-8") as f:
    json.dump(result, f, ensure_ascii=False, indent=4)
print(result)
